package guinong.com.gnw.base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.zhy.autolayout.AutoLayoutActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;
import guinong.com.gnw.R;
import guinong.com.gnw.core.GuiNongAppManager;
import guinong.com.gnw.port.MainUsedInterface;
import guinong.com.gnw.ui.widged.CustomProgressDialog;
import guinong.com.gnw.utils.L;
import guinong.com.gnw.utils.SharedPreferencesUtils;
import guinong.com.gnw.utils.T;

/**
 * @author chenshuangniu
 * @time 2016/4/15 16:50
 * @内容 activity的基类
 * @版本
 */
public abstract class BaseActivity<P extends BasePresenter> extends AutoLayoutActivity implements MainUsedInterface.MainButtonClickListener {
    private static final String TAG = "BaseActivity";
    protected P mPresenter;
    protected Context mContext;
    protected Dialog progressDialog;

    /**
     * shardePrefrences  数据存储
     */
    protected SharedPreferencesUtils mShareDataUtils;
    /**
     * 标记是否可以显示Dilaog
     */
    private boolean isCanShowDialog = true;

    /**
     * layout id
     */
    protected abstract int getLayoutId();

    /**
     * 需要请求mvp网络数据时重写
     */
    protected void initPresenter() {
    }

    /**
     * 初始化数据的  视图
     *
     * @param savedInstanceState
     */
    protected void initView(Bundle savedInstanceState) {

    }

    /**
     * titileBar设置
     */
    protected abstract void setTitle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L.e("start:" + getClass().getSimpleName());
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mContext = this;
        GuiNongAppManager.getInstance(mContext).addActivity(this);
        mShareDataUtils = SharedPreferencesUtils.getInstance(mContext);
        initPresenter();
        setTitle();
        initView(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    /**
     * 吐司
     **/
    public void toastShortBasic(String msg) {
        T.showShort(mContext, msg);
    }

    public void toastLongBasic(String msg) {
        T.showLong(mContext, msg);
    }

    public void showLoading() {
        showLoading(getResources().getString(R.string.loading_normal_str));
    }

    /**
     * 等待框
     **/
    public void showLoading(String msg) {
        stopLoading();
        progressDialog = CustomProgressDialog.createDialog(mContext, msg);
        progressDialog.show();
    }

    /**
     * 停止等待框
     **/
    public void stopLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    /***
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /***
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, String type) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("type", type);
        startActivity(intent);
    }


    protected void startActivity(String action, Uri uri) {
        Intent intent = new Intent(action, uri);
        startActivity(intent);
    }
    /**
     * 含有Bundle通过Action跳转界面
     **/
    protected void startActivity(String action, Bundle bundle) {
        Intent intent = new Intent();
        intent.setAction(action);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Subscribe
    public void showToast(String msg) {
        T.showShort(this, msg);
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLoading();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        EventBus.getDefault().unregister(this);
    }


    /**
     * used to check the network is available or not
     *
     * @return
     */
    public boolean checkNetwork() {
        ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}